<?php

/**

 * 前台注册

 *

 *

 *

 * * @网店运维 (c) 2015-2018  Inc. (http://www.shopwwi.com)
 * @license    http://www.shopwwi.c om
 * @link       交流群号：111731672
 * @since      网店运维提供技术支持 授权请购买shopnc授权

 */



//use Shopnc\Tpl;



defined('InShopNC') or exit('Access Invalid!');



class registerControl extends mobileHomeControl
{


    public function __construct()
    {

        parent::__construct();

    }

    /*
     * 矿博会注册页面
     */
    public function indexOp()
    {
        Language::read("home_login_register");
        $lang = Language::getLangContent();
        $model_member = Model('member');
        if (chksubmit()) {
            $mobile = trim($_POST['mobile']);
            if (!empty($mobile) && preg_match('/^0?(13|15|17|18|14)[0-9]{9}$/i', $mobile)) {
                if (!empty($_POST['captcha'])) {
                    $captcha = trim($_POST['captcha']);
                    if ($this->check_captcha($mobile, $captcha)) {
                        //注册码验证通过
                        if (C('assign_user_name')) {
                            $number = $model_member->getRegisterNum();
                            $NumberLogic = Logic("number");
                            $user_id = substr(date('Ym'), 1, 5) . sprintf("%06s", $number + 1);
                            $user_code = $NumberLogic->encodeNum($user_id);
                            $member_name = "JYC" . substr($user_code, 1);
                            $member_name = $model_member->getCheckName($number,$member_name);
                        }

                        $passwd = $captcha;
                        $truename = trim($_POST['truename']);
                        $member = $model_member->getMemberInfo(array('member_name' => $member_name));//检查重名

                        if (!empty($member)) {
                            mobileJump('用户名已被注册', 'index.php?act=register');
                        }
                        $member = $model_member->getMemberInfo(array('member_mobile' => $mobile));//检查手机号是否已被注册
                        if (!empty($member)) {
                            mobileJump('手机号已被注册', 'index.php?act=register');
                        }
                        if (C('ucenter_status')) {
                           /* $model_ucenter = Model('ucenter');
                            $remote_member_info = $model_ucenter->getUserInfo(trim($mobile), 4); */
                            $newuc=Model('newuc');
                            $remote_member_info=$newuc->get_uc_user_info(array('user_name'=>$mobile));
                        }

                        if (!empty($remote_member_info)) {
                            //UC用户直接写入数据表
                            $member = array();
                            $member['member_id'] = $remote_member_info['uid'];
                            $member['member_name'] = $remote_member_info['username'];
                            $member['member_passwd'] = $remote_member_info['password'];
                            $member['member_mobile'] = $remote_member_info['mobile'];
                            $member['member_email'] = $remote_member_info['email'];
                            $member['member_nickname'] = "u" . substr($mobile, -4);
                            $member['member_truename'] = $remote_member_info['truename'];
                            $member['member_mobile_bind'] = 1;
                            $member['plain_passwd'] = 1;
                            $member['member_type'] = $remote_member_info['usertype'];
                            $ref_member = cookie('ref_member');
                            if(!empty($ref_member)){
                                $member['member_invite'] = $ref_member;
                            }
                            $result = $model_member->addMember($member);
                            if ($result) {
                                $member_name = $remote_member_info['username'];
                                $member = $model_member->getMemberInfo(array('member_name' => $member_name));
                                $model_member->createSession($member, true);//自动登录
                                redirect('index.php?act=register&op=success&ref='.$member_name);
                            }
                        } else {
                            /**
                             * Ucenter处理
                             */
                            if (C('ucenter_status')) {
                                Language::read("home_login_register");
                                $lang = Language::getLangContent();
                                $data = array();
                                $data['username']=$member_name;
                                $data['truename'] = $truename;
                                $data['password']=$passwd;
                                $data['mobile']=$mobile;
                                //var_dump($data);exit;
                                $newuc=Model('newuc');
                                $uid=$newuc->uc_mobile_reg($data);  //UID
                                /*  $model_ucenter = Model('ucenter');
                                  $uid = $model_ucenter->addUser(array('username' => $member_name, 'password' => $passwd, 'mobile' => trim($mobile), 'email' => '', 'member_type' => 2));*/

                                //if($uid<1) showMessage($lang['login_usersave_regist_fail'],'','html','error');
                            }
                            if($uid){
                                $remote_member_info=$newuc->get_uc_user_info(array('user_name'=>$mobile));
                            }else{
                                mobileJump('网络忙,请稍后再试');
                            }
                            $member = array();
                            $member['member_id'] = $remote_member_info['uid'];
                            $member['member_number'] = $number;
                            $member['member_name'] = $member_name;
                            $member['member_passwd'] = $passwd;
                            $member['member_mobile'] = $mobile;
                            $member['member_email'] = '';
                            $member['member_mobile_bind'] = 1;
                            $member['member_nickname'] = "u" . substr($mobile, -4);
                            $member['member_truename'] = trim($_POST['truename']);
                            $member['member_type'] = $remote_member_info['usertype'];
                            $ref_member = cookie('ref_member');
                            if(!empty($ref_member)){
                                $member['member_invite'] = $ref_member;
                            }
                            $result = $model_member->addMember($member);
                            if ($result) {
                                $member = $model_member->getMemberInfo(array('member_name' => $member_name));
                                $model_member->createSession($member, true);//自动登录
                                redirect('index.php?act=register&op=success&ref='.$member_name);
                            } else {
                                mobileJump('注册失败', 'index.php?act=register');
                            }
                        }
                    } else {
                        mobileJump('验证码不正确', 'index.php?act=register');
                    }
                }else {
                    Tpl::output('mobile', $mobile);
                    Tpl::output('truename', $_POST['truename']);
                }
            } else {
                mobileJump('手机号格式不正确', 'index.php?act=register');
            }
        }
        $model_member->checkloginMember();
        Tpl::output('html_title', C('site_name') . ' - ' . $lang['login_register_join_us']);
        if(!empty($_GET['ref'])){
            setNcCookie('ref_member',$_GET['ref'],3600*24);//COOKIE
        }
        Tpl::showpage('reg');
    }

    public function wifiOp()
    {
        $model_member = Model('member');
        $mobile = trim($_GET['mobile']);
        $truename = trim($_GET['name']);
        if (!empty($mobile) && preg_match('/^0?(13|15|17|18|14)[0-9]{9}$/i', $mobile)) {
                $member = $model_member->getMemberInfo(array('member_mobile' => $mobile));//检查手机号是否已被注册
                if(empty($member)) { //未注册用户直接注册
                    //注册码验证通过
                    if (C('assign_user_name')) {
                        $number = $model_member->getRegisterNum();
                        $NumberLogic = Logic("number");
                        $user_id = substr(date('Ym'), 1, 5) . sprintf("%06s", $number + 1);
                        $user_code = $NumberLogic->encodeNum($user_id);
                        $member_name = "JYC" . substr($user_code, 1);
                        $member_name = $model_member->getCheckName($number, $member_name);
                    }

                    $captcha = rand(100000, 999999);
                    $log_msg = '欢迎您' . date("Y-m-d");
                    $log_msg .= '注册【' . C('site_name') . '】会员，登入密码是：' . $captcha . '。';
                    $sms = new Sms();
                    $result = $sms->send($mobile, $log_msg);
                    $model_sms_log = Model('sms_log');
                    $log_array['log_phone'] = $mobile;
                    $log_array['log_captcha'] = $captcha;
                    $log_array['log_ip'] = getIp();
                    $log_array['log_msg'] = $log_msg." WIFI";
                    $log_array['log_type'] = 1;
                    $log_array['add_time'] = time();
                    $model_sms_log->addSms($log_array);

                    $passwd = $captcha;

                    if (C('ucenter_status')) {
                        /* $model_ucenter = Model('ucenter');
                         $remote_member_info = $model_ucenter->getUserInfo(trim($mobile), 4); */
                        $newuc = Model('newuc');
                        $remote_member_info = $newuc->get_uc_user_info(array('user_name' => $mobile));
                    }

                    if (!empty($remote_member_info)) {
                        //UC用户直接写入数据表
                        $member = array();
                        $member['member_id'] = $remote_member_info['uid'];
                        $member['member_name'] = $remote_member_info['username'];
                        $member['member_passwd'] = $remote_member_info['password'];
                        $member['member_mobile'] = $remote_member_info['mobile'];
                        $member['member_email'] = $remote_member_info['email'];
                        $member['member_nickname'] = "u" . substr($mobile, -4);
                        $member['member_truename'] = $truename;
                        $member['member_mobile_bind'] = 1;
                        $member['plain_passwd'] = 1;
                        $member['member_type'] = $remote_member_info['usertype'];
                        $ref_member = cookie('ref_member');
                        if (!empty($ref_member)) {
                            $member['member_invite'] = $ref_member;
                        }
                        $result = $model_member->addMember($member);
                        if ($result) {
                            $member_name = $remote_member_info['username'];
                            $member = $model_member->getMemberInfo(array('member_name' => $member_name));
                            $model_member->createSession($member, true);//自动登录
                        }
                    } else {
                        /**
                         * Ucenter处理
                         */
                        if (C('ucenter_status')) {
                            Language::read("home_login_register");
                            $lang = Language::getLangContent();
                            $data = array();
                            $data['username'] = $member_name;
                            $data['truename'] = $truename;
                            $data['password'] = $passwd;
                            $data['mobile'] = $mobile;
                            //var_dump($data);exit;
                            $newuc = Model('newuc');
                            $uid = $newuc->uc_mobile_reg($data);  //UID
                            /*  $model_ucenter = Model('ucenter');
                              $uid = $model_ucenter->addUser(array('username' => $member_name, 'password' => $passwd, 'mobile' => trim($mobile), 'email' => '', 'member_type' => 2));*/

                            //if($uid<1) showMessage($lang['login_usersave_regist_fail'],'','html','error');
                        }
                        if ($uid) {
                            $remote_member_info = $newuc->get_uc_user_info(array('user_name' => $mobile));
                        }
                        $member = array();
                        $member['member_id'] = $remote_member_info['uid'];
                        $member['member_number'] = $number;
                        $member['member_name'] = $member_name;
                        $member['member_passwd'] = $passwd;
                        $member['member_mobile'] = $mobile;
                        $member['member_email'] = '';
                        $member['member_mobile_bind'] = 1;
                        $member['member_nickname'] = "u" . substr($mobile, -4);
                        $member['member_truename'] = $truename;
                        $member['member_type'] = $remote_member_info['usertype'];
                        $ref_member = cookie('ref_member');
                        if (!empty($ref_member)) {
                            $member['member_invite'] = $ref_member;
                        }
                        $result = $model_member->addMember($member);
                        if ($result) {
                            $member = $model_member->getMemberInfo(array('member_name' => $member_name));
                            $model_member->createSession($member, true);//自动登录
                        }
                    }
                }else{
                    $result = true;
                    /*
                    $captcha = rand(100000, 999999);
                    $log_msg = '欢迎您回来' . date("Y-m-d");
                    $log_msg .= '【' . C('site_name') . '】会员，登入密码是：' . $captcha . '。';
                    $sms = new Sms();
                    $result = $sms->send($mobile, $log_msg);
                    $model_sms_log = Model('sms_log');
                    $log_array['log_phone'] = $mobile;
                    $log_array['log_captcha'] = $captcha;
                    $log_array['log_ip'] = getIp();
                    $log_array['log_msg'] = $log_msg;
                    $log_array['log_type'] = 1;
                    $log_array['add_time'] = time();
                    $model_sms_log->addSms($log_array);
                    $result = $model_member->editMember(array('member_id'=>$member['member_id']),array('member_passwd'=>md5($captcha)));
                    if($result && C('ucenter_status')){
                        $member = $model_member->getMemberInfo(array('member_id'=>$member['member_id']));
                        $newuc=Model('newuc');
                        $newuc->update_uc_passwd(md5($captcha),$member['member_id']);
                    }
                    */
                }
              if ($result) echo "ok";
             else echo "false";
        }else{
            echo "false";
        }
    }

    public function successOp()
    {
        $ref = $_GET['ref'];
        if(empty($_SESSION['member_id'])){
            mobileJump('跳转注册', 'index.php?act=register&op=index&ref='.$ref);
        }
        Tpl::output('ref',$ref);
        Tpl::showpage('mobile_success');
    }

    /**
     * 验证注册动态码
     */
    private function check_captcha($phone, $captcha)
    {
        $state = false;
        if (strlen($phone) == 11 && strlen($captcha) == 6) {
            $condition = array();
            $condition['log_phone'] = $phone;
            $condition['log_captcha'] = $captcha;
            $condition['log_type'] = 1;
            $model_sms_log = Model('sms_log');
            $sms_log = $model_sms_log->getSmsInfo($condition);
            if (!empty($sms_log) && ($sms_log['add_time'] > TIMESTAMP - 1800)) {//半小时内进行验证为有效
                $state = true;
            }
        }
        return $state;
    }


    /**
     * 退出操作
     *
     * @param int $id 记录ID
     * @return array $rs_row 返回数组形式的查询结果
     */
    public function logoutOp()
    {
        Language::read("home_login_index");
        $lang = Language::getLangContent();
        // 清理COOKIE
        $data['uid']=$_SESSION['member_id'];
        setNcCookie('msgnewnum' . $_SESSION['member_id'], '', -3600);
        setNcCookie('auto_login', '', -3600);
        setNcCookie('cart_goods_num', '', -3600);
        session_unset();
        session_destroy();
        /**
         * 同步登录通知
         */
        /**
         * Ucenter处理
         */

        if (C('ucenter_status')) {
            $newuc=Model('newuc');
            $newuc->synlogout($data);
          /*  $model_ucenter = Model('ucenter');
            $out_str = $model_ucenter->userLogout();*/
            $out_str = '';

            $lang['login_logout_success'] = $lang['login_logout_success'] . $out_str;
            if (empty($_GET['ref_url'])) {
                $ref_url = getReferer();
            } else {
                $ref_url = $_GET['ref_url'];
            }
            mobileJump('登出成功', 'index.php?act=login&ref_url=' . urlencode($ref_url));
        } else {
            redirect();
        }
    }
}
